package com.jin.webvideo.externalInterfaces.video;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jin.webvideo.base.util.CollectionsUtil;
import com.jin.webvideo.exception.TakenDownException;
import com.jin.webvideo.exception.UnavailableServiceException;
import com.jin.webvideo.externalInterfaces.PlatformSupport;
import com.jin.webvideo.externalInterfaces.entity.Filmography;
import com.jin.webvideo.externalInterfaces.entity.FilmographyInfo;
import com.jin.webvideo.util.sys.ClientUtil;
import com.jin.webvideo.util.base.URLUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class ImgoApi extends AbstractVideoAPI {

    private static final String URL = "https://pcweb.api.mgtv.com/list/master?_support=10000000&filterpre=true&vid=&cid=%docid%&pn=%pageNum%&ps=%pageSize%&month=";

    @Override
    public Filmography baseInfo(String docid, String tvid) throws UnavailableServiceException {
        try {
            return baseInfoFromNet(docid, tvid);
        } catch (IOException e) {
            e.printStackTrace();
            throw new UnavailableServiceException();
        }
    }

    @Override
    public List<FilmographyInfo> videoInfoList(String docid, String tvid, Integer pageNo, Integer pageSize) throws UnavailableServiceException {
        try {
            List<FilmographyInfo> list = videoInfos(docid, tvid, String.valueOf(pageNo), String.valueOf(pageSize));
            if (CollectionsUtil.isEmpty(list)) {
                throw new TakenDownException();
            }
            return list;
        } catch (IOException e) {
            e.printStackTrace();
            throw new UnavailableServiceException();
        }
    }

    @Override
    String platformName() {
        return PlatformSupport.PLATFORM_IMGO;
    }


    private List<FilmographyInfo> videoInfos(String docid, String tvid, String pageNum, String pageSize) throws IOException {
        List<FilmographyInfo> filmographyInfos = new ArrayList<>();
        String url = URLUtil.format(URL, docid, pageNum, pageSize);
        String json = ClientUtil.httpGet(url);
        JSONObject obj = JSONObject.parseObject(json);
        if (obj.getInteger("code") == 200){
            obj = obj.getJSONObject("data");
            JSONArray list = obj.getJSONArray("list");
            filmographyInfos = parseVideoInfo(list);
        }
        return filmographyInfos;
    }

    private Filmography baseInfoFromNet(String docid, String tvid) throws IOException {
        Filmography filmography = new Filmography();
        filmography.setPlatformName(PlatformSupport.PLATFORM_NAME_IMGO);
        filmography.setPlatform(PlatformSupport.PLATFORM_IMGO);
        filmography.setId(docid);
        String html = ClientUtil.httpGet(String.format("https://www.mgtv.com/b/%s/%s.html", docid, tvid));
        Document doc = Jsoup.parse(html);
        Elements descDoms = doc.head().getElementsByAttributeValue("name", "description");
        if (descDoms.size() > 0){
            String desc = descDoms.get(0).attr("content");
            filmography.setDesc(desc);
        }
        descDoms = doc.head().getElementsByAttributeValue("name", "keywords");
        if (descDoms.size() > 0){
            String keywords = descDoms.get(0).attr("content");
            if (null != keywords){
                int index = keywords.indexOf(" ");
                if (index != -1){
                    keywords = keywords.substring(0, index);
                }
                filmography.setName(keywords);
            }
        }
        return filmography;
    }
    private List<FilmographyInfo> parseVideoInfo(JSONArray array){
        List<FilmographyInfo> filmographyInfos = new ArrayList<>();

        for (int i = 0, l = array.size(); i < l; i ++){
            JSONObject info = array.getJSONObject(i);
            FilmographyInfo filmographyInfo = new FilmographyInfo();
            filmographyInfo.setId( info.getString("video_id") );
            filmographyInfo.setUrl( formatURL(info.getString("url"))  );
            filmographyInfo.setPlatform(PlatformSupport.PLATFORM_IMGO);
            filmographyInfo.setTitle(info.getString("t2") + " " + info.getString("t1") );
            filmographyInfo.setDate(info.getString("ts"));
            filmographyInfo.setDesc(info.getString("t3"));
            filmographyInfo.setDisplayMode(FilmographyInfo.DISPLAY_MODE_TITLE_BLOCK);
            filmographyInfo.setImg(info.getString("img"));
            filmographyInfo.setNumber(i+1);
            filmographyInfos.add(filmographyInfo);
        }
        return filmographyInfos;
    }
    private String formatURL(String path){
        if (path.startsWith("/")){
            path = path.substring(1);
        }
        return "https://www.mgtv.com/" + path;
    }
}
